function findReelsByTime(url) {
    var day = $('#day').val();
    var storageUuid = $('#storageUuid').val();
    var condition = $.trim($('#condition').val());
    var subMaterielId = $('#subMaterielId').val();
    console.log(condition+'1');
    var range = $('#range').val();
    if(storageUuid === '' || storageUuid.length === 0){
        layer.msg('请选择仓库');
        return ;
    }
    if(condition === '' || condition.length === 0){
        layer.msg('请选择范围');
        return ;
    }
    var reg = /^[0-9]\d*$/;
    var flag = reg.test(day);
    console.log(flag);
    if(!flag){
        layer.msg('滞留天数请输入整数');
        return ;
    }
    $.ajax({
        url:url,
        dataType:"json",
        type:'post',
        data:{day:day,condition:condition,storageUuid:storageUuid,range:range,type:'retention',subMaterielId:subMaterielId},
        beforeSend:function () {
            index =  layer.load(0, {shade: false});
        }, complete:function(){
            layer.close(index);
        },success:function(res){
            if(res.success===true){
                layer.msg(res.message);
                showReels(res.data);
            }else if(res.success === false){
                layer.msg(res.message);
            }
        },error:function(){
            layer.msg('网络错误');
        }
    });
}

function findReelsByBatch(url) {
    var reel = $('#reel').val();
    var storageUuid = $('#storageUuid').val();
    if(storageUuid === '' || storageUuid.length === 0){
        layer.msg('请选择仓库');
        return ;
    }
    if(reel.length === 0 || reel === ''){
        layer.msg('批次号不能为空');
        return ;
    }else if(reel.length !== 12){
        layer.msg('批次号长度应该为12');
        return ;
    }
    $.ajax({
        url:url,
        dataType:"json",
        type:'post',
        data:{reel:reel,storageUuid:storageUuid,type:'batch'},
        beforeSend:function () {
            index =  layer.load(0, {shade: false});
        }, complete:function(){
            layer.close(index);
        },success:function(res){
            if(res.success===true){
                layer.msg(res.message);
                showReels(res.data);
            }else if(res.success === false){
                layer.msg(res.message);
            }
        },error:function(){
            layer.msg('网络错误');
        }
    });
}

function createBom(url) {
    var storageUuid = $('#storageUuid').val();
    console.log(storageUuid);
    if(storageUuid === '' || storageUuid.length === 0){
        layer.msg('请选择仓库');
        return ;
    }
    var data = [];
    var i=0;
    $('.reelId').each(function (index,value,row) {
        if(this.checked){
            data[i++] = $(this).data('reel');
        }
    });
    var length = data.length;
    console.log('勾选条码条数:'+ length);
    if(data.length === 0){
        layer.alert('请勾选所需数据');
        return;
    }
     data = JSON.stringify(data);
    // console.log(data);
    $.ajax({
        url:url,
        dataType:"json",
        type:'post',
        data:{reels:data,storageUuid:storageUuid},
        beforeSend:function () {
            index =  layer.load(0, {shade: false});
        }, complete:function(){
            layer.close(index);
        },success:function(res){
            if(res.success===true){
                layer.msg(res.message);
            }else if(res.success === false){
                layer.msg(res.message);
            }
        },error:function(){
            layer.msg('网络错误');
        }
    });
}
function showReels(data) {
    var html = '';
    html +='<thead class="text-center">\n' +
        '                <tr>\n' +
        '                    <th><input type="checkbox" class="select-all" checked onchange="selectAll()"/>全选</th>\n' +
        '                    <th>序号</th>\n' +
        '                    <th>条码</th>\n' +
        '                    <th>物料号</th>\n' +
        '                    <th>数量</th>\n' +
        '                    <th>站位</th>\n' +
        '                    <th>容器</th>\n' +
        '                    <th>时间</th>\n' +
        '                    <th>天数</th>\n' +
        '                </tr>\n' +
        '                </thead>';
    html +='<tbody>';
    for(var i = 0 ;i<data.length;i++){
        var item = data[i];
        if(item.container_code == null){
            item.container_code = '';
        }
        html +='<tr>'+
            '<td><input type="checkbox" name="reelId" class="reelId" data-reel='+item.reel_id+' checked></td>'+
            '<td>'+(i+1)+'</td>'+
            '<td>'+item.reel_id+'</td>'+
            '<td>'+item.sub_materiel_id+'</td>'+
            '<td>'+item.count+'</td>'+
            '<td>'+item.seat_code+'</td>'+
            '<td>'+item.container_code+'</td>'+
            '<td>'+item.time+'</td>'+
            '<td>'+item.day+'</td>'+
            '</tr>';
    }
    html +='</tbody>';
    $('#but').show();
    $('.table').empty().append(html);
}
function selectAll() {
    $('.reelId').each(function (index,value,row) {
        console.log(index);
        this.checked = !this.checked;
    });
}

// function downloadExcel(url,type) {
//     console.log(type);
//     var storageUuid = $('#storageUuid').val();
//     if(storageUuid === '' || storageUuid.length === 0){
//         layer.msg('请选择仓库');
//         return ;
//     }
//     var data = [];
//     var i=0;
//     $('.reelId').each(function (index,value,row) {
//         if(this.checked){
//             data[i++] = $(this).data('reel');
//         }
//     });
//     console.log(data.length);
//     if(data.length === 0){
//         layer.alert('请勾选所需数据');
//         return;
//     }
//     $.ajax({
//         url:url,
//         dataType:"json",
//         type:'post',
//         data:{reels:data,storageUuid:storageUuid,type:type},
//         beforeSend:function () {
//             index =  layer.load(0, {shade: false});
//         }, complete:function(){
//             layer.close(index);
//         },success:function(res){
//             if(res.success===true){
//                 var uuid = res.data;
//                 console.log('开始下载excel');
//                 url = url+'&uuid='+uuid;
//                 window.location.href = url;
//                 console.log(url);
//             }else if(res.success === false){
//                 layer.msg(res.message);
//             }
//         },error:function(){
//             layer.msg('网络错误');
//         }
//     });
// }

function downloadExcel(url,type) {
    var storageUuid = $('#storageUuid').val();
    if(storageUuid === '' || storageUuid.length === 0){
        layer.msg('请选择仓库');
        return ;
    }
    var data = [];
    var i=0;
    $('.reelId').each(function (index,value,row) {
        if(this.checked){
            data[i++] = $(this).data('reel');
        }
    });
    if(data.length === 0){
        layer.alert('请勾选所需数据');
        return;
    }
    data = JSON.stringify(data);
    $.ajax({
        url:url,
        dataType:"json",
        type:'post',
        data:{reels:data,storageUuid:storageUuid,type:type},
        beforeSend:function () {
            index =  layer.load(0, {shade: false});
        }, complete:function(){
            layer.close(index);
        },success:function(res){
            if(res.success===true){
                var $a = $("<a>");
                $a.attr("href", res.data.file);
                $a.attr("download", res.data.filename);
                $("body").append($a);
                $a[0].click();
                $a.remove();
            }else if(res.success === false){
                layer.msg(res.message);
            }
        },error:function(){
            layer.msg('网络错误');
        }
    });
}